import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.ConnectException;
import java.net.Socket;

import com.acme.bankapp.domain.*;
public class BankClient {
	public static void main(String[] args) {
		try {
			Socket s = new Socket("localhost", 90);
			PrintWriter out = new PrintWriter(new OutputStreamWriter((s.getOutputStream())), true);
			out.println("add accounttype=c;balance=19;overdraft=50;firstname=K;lastname=K1;gender=m;");
			//out.println("printall");
			try {
				FileOutputStream f = new FileOutputStream("bank.ser");
				ObjectOutputStream outObj = new ObjectOutputStream(f);
				ObjectInputStream inObj = new ObjectInputStream(s.getInputStream());
				try {
					Client object = (Client)inObj.readObject();
					while (object != null)
					{
						outObj.writeObject(object);
						object = (Client)inObj.readObject();
					}
				} catch (IOException e) {
					System.out.println("It's end of stream");
				}
				outObj.flush();
				inObj.close();
				outObj.close();
				f.close();
			}
			catch (ClassNotFoundException e) {
				System.out.println("Serialization in bank.ser is not successful");
			}
			s.close();
		}
		catch (ConnectException e) {
			System.out.println("There is no connect");
		}
		catch (IOException e) {
			System.out.println("Error");
		}
		
	}
}
